package com.cn.yugan.Activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.format.DateUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.cn.yugan.Cons.ArticleConstants;
import com.cn.yugan.Cons.LoginConstants;
import com.cn.yugan.Engine.Adapter.MarkListViewAdpater;
import com.cn.yugan.Engine.Beans.ArticleListInfo;
import com.cn.yugan.Engine.Beans.UserInfo;
import com.cn.yugan.Engine.Http.GetList;
import com.cn.yugan.Engine.Json.ArticleListJson;
import com.cn.yugan.Engine.Json.UserInfoJson;
import com.cn.yugan.Utils.LogUtils;
import com.cn.yugan.Utils.ToastUtils;
import com.cn.yugan.mobile.R;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.apache.http.Header;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Rover on 2016/7/30 .
 * Email 357319517@qq.com
 */
public class MarkActivity extends AppCompatActivity {
    private PullToRefreshListView mPullRefreshListView;
    private MarkListViewAdpater madapter;
    private ArrayList<UserInfo> mlist;
    private int page = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mark);
        initActionbar();
        loadData();
        init();
    }

    private void init() {
        mlist = new ArrayList<>();
        mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.mark_lv);
        madapter = new MarkListViewAdpater(this, mlist);
        mPullRefreshListView.setAdapter(madapter);
        mPullRefreshListView.setMode(Mode.PULL_FROM_END);
        mPullRefreshListView.setOnRefreshListener(new OnRefreshListener());
        ILoadingLayout endLabels = mPullRefreshListView.getLoadingLayoutProxy(
                false, true);
        endLabels.setPullLabel("上拉我刷新(*^__^*) ");// 刚下拉时，显示的提示    
        endLabels.setRefreshingLabel("鱼干娘寻找中...");// 刷新时    
        endLabels.setReleaseLabel("放开我刷新((‵□′)) ");// 下来达到一定距离时，显示的提示
        mPullRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    UserInfo userInfo = (UserInfo) madapter.getItem(position - 1);
                    //相关操作
                    Intent intent = new Intent(MarkActivity.this, UpinfoActivity.class);
                    intent.putExtra("uid", userInfo.getUid());
                    startActivity(intent);
            }
        });
    }

    private void loadData() {
        GetList.VoteListAsyncHttpClient(this, getString(R.string.serverurl) + getString(R.string.getinfos), 1,new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    LogUtils.d(new String(bytes, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                mlist = UserInfoJson.json2list(bytes);
                madapter.refreshList(mlist);
                madapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                ToastUtils.show(MarkActivity.this, "服务器未响应");
            }
        });
    }

    private void initActionbar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("我的关注");
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.action_nomal_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                break;
        }
        return true;
    }

    private class OnRefreshListener implements PullToRefreshBase.OnRefreshListener<ListView> {
        @Override
        public void onRefresh(final PullToRefreshBase<ListView> refreshView) {
            if (mPullRefreshListView.isFooterShown()) {
                page++;
                GetList.VoteListAsyncHttpClient(MarkActivity.this, getString(R.string.serverurl) + getString(R.string.getinfos), page, new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int i, Header[] headers, byte[] bytes) {
                        //  LogUtils.d(new String(bytes, "UTF-8"));
                        mlist = UserInfoJson.json2list(bytes);
                        if (mlist == null) {
                            page--;
                        } else {
                            madapter.addList(mlist);
                            madapter.notifyDataSetChanged();
                        }
                        refreshView.onRefreshComplete();
                    }

                    @Override
                    public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                        ToastUtils.show(MarkActivity.this, "服务器未响应");
                        page--;
                        refreshView.onRefreshComplete();
                    }
                });

            }
        }

    }
}

